﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace inf_1007_devoir3
{
    public partial class frmPaiementRetour : Form
    {
        private Location location;
        private Location oldLocation;
        private frmListeLocations frmListe;
        bool termine;

        public frmPaiementRetour(frmListeLocations frmListe, Location location, Location oldLocation)
        {
            this.frmListe = frmListe;
            this.location = location;
            this.oldLocation = oldLocation;
            termine = false;
            InitializeComponent();
        }

        private void frmPaiementRetour_Load(object sender, EventArgs e)
        {
            lblKilometrageTotal.Text = location.getKilometrageTotal().ToString();
            lblKilometrageInclus.Text = location.getKilometrageInclus().ToString();
            lblKilometrageSurplus.Text = location.getKilometrageSurplus().ToString();
            lblMontantSurplus.Text = String.Format("{0:0.00}", location.getMontantKilometrageSurplus()) + "$";
            lblMontantDegats.Text = String.Format("{0:0.00}", location.getMontantDegats()) + "$";
            lblTPS.Text = String.Format("{0:0.00}", location.calculTPSRetour()) + "$";
            lblTVQ.Text = String.Format("{0:0.00}", location.calculTVQRetour()) + "$";
            lblDepotGarantie.Text = String.Format("{0:0.00}", location.depotGarantie()) + "$";
            lblMontantAPayer.Text = String.Format("{0:0.00}", location.calculMontantTotalRetour()) + "$";
            
            if (location.calculMontantTotalRetour() < 0)
            {
                lblMontantPaye.Visible = false;
                txtMontantPaye.Visible = false;
            }
        }

        private void btnAnnuler_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPaiementRetour_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (oldLocation != null && !termine)
            {
                oldLocation.copier(location);
            }
        }

        private void btnTerminer_Click(object sender, EventArgs e)
        {
            if (valider())
            {
                termine = true;
                CtrlLocation.getInstance().payerRetour(location);
                frmListe.remplirListe();
                this.Close();
            }
        }

        private bool valider()
        {
            bool valide = true;

            error.Clear();

            if (location.calculMontantTotalRetour() > 0)
            { 
                double montantPaye = -1;

                try
                {
                    montantPaye = Convert.ToDouble(txtMontantPaye.Text.Trim());
                }
                catch (Exception e)
                {
                    error.SetError(txtMontantPaye, "Veuillez entrer un montant au format numérique.");
                    valide = false;
                }

                if (montantPaye != -1)
                {
                    if (montantPaye != Math.Round(location.calculMontantTotalRetour(), 2))
                    { 
                        error.SetError(txtMontantPaye, "Veuillez payer en entier le montant.");
                        valide = false;
                    }
                    
                }
            }

            return valide;
        }
    }
}
